package com.ruoyi.answer.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.alibaba.excel.EasyExcel;
import com.ruoyi.answer.domain.excel.ExcelPaper;
import com.ruoyi.common.annotation.Anonymous;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.answer.domain.vo.ExamPaperVo;
import com.ruoyi.answer.domain.bo.ExamPaperBo;
import com.ruoyi.answer.service.IExamPaperService;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 试卷管理
 *
 * @author kun_er
 * @date 2022-08-16
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/answer/paper")
public class ExamPaperController extends BaseController {

    private final IExamPaperService iExamPaperService;

    /**
     * 查询试卷管理列表
     */
//    @SaCheckPermission("answer:paper:list")
    @Anonymous
    @GetMapping("/list")
    public TableDataInfo<ExamPaperVo> list(ExamPaperBo bo, PageQuery pageQuery) {
        return iExamPaperService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出试卷管理列表
     */
    @SaCheckPermission("answer:paper:export")
    @Log(title = "试卷管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ExamPaperBo bo, HttpServletResponse response) {
        ExcelUtil.exportExcel(null, "试卷管理", ExcelPaper.class, response);
    }

    /**
     * 导入试卷
     */
    @PostMapping("addPaperByExcel")
    public R addPaperByExcel(MultipartFile file) throws IOException {
        InputStream inputStream = file.getInputStream();
        String name = file.getOriginalFilename();
        List<ExcelPaper> list = EasyExcel.read(inputStream).headRowNumber(1).head(ExcelPaper.class).sheet().doReadSync();

        if (list == null || list.size() == 0) {

            return R.fail("导入失败，没有读取到导入数据！");
        } else {
            // service层
            return iExamPaperService.importCustomer(list,name);
        }
    }

    /**
     * 获取试卷管理详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("answer:paper:query")
    @Anonymous
    @GetMapping("/{id}")
    public R<ExamPaperVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iExamPaperService.queryById(id));
    }

    /**
     * 新增试卷管理
     */
    @SaCheckPermission("answer:paper:add")
    @Log(title = "试卷管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ExamPaperBo bo) {
        return toAjax(iExamPaperService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改试卷管理
     */
    @SaCheckPermission("answer:paper:edit")
    @Log(title = "试卷管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ExamPaperBo bo) {
        return toAjax(iExamPaperService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除试卷管理
     *
     * @param ids 主键串
     */
    @SaCheckPermission("answer:paper:remove")
    @Log(title = "试卷管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iExamPaperService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
